﻿using System;
using HtmlAgilityPack;

namespace 第五次作业.utils;

using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class SearchScript
{
    private readonly HttpClient _httpClient;

    public SearchScript()
    {
        _httpClient = new HttpClient();
    }

    public async Task<List<string>> Search(string keyword, int maxResults)
    {
        List<string> results = new List<string>();
        string bingSearchUrl = $"https://www.bing.com/search?q={keyword}";
        
        var bingTask = ParseUrlsFromSearchEngine(bingSearchUrl, maxResults);

        await bingTask;
        
        results.AddRange(bingTask.Result);

        return results;
    }

    private async Task<List<string>> ParseUrlsFromSearchEngine(string searchUrl, int maxResults)
    {
        List<string> urls = new List<string>();

        string html = await _httpClient.GetStringAsync(searchUrl);
        // Regex linkRegex = new Regex(@"<a\s+(?:[^>]*?\s+)?href=([""'])(.*?)\1",
        //     RegexOptions.IgnoreCase | RegexOptions.Compiled);
        // var matches = linkRegex.Matches(html);
        
        List<string> links = ExtractLinks(html);

        foreach (string link in links)
        {
            if (!urls.Contains(link) && urls.Count < maxResults)
            {
                urls.Add(link);
            }
        }

        return urls;
    }
    
    public static List<string> ExtractLinks(string html)
    {
        List<string> links = new List<string>();

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);

        foreach (var link in doc.DocumentNode.Descendants("a"))
        {
            string href = link.GetAttributeValue("href", null);
            if (!string.IsNullOrEmpty(href) && IsHttpOrHttpsLink(href))
            {
                links.Add(href);
            }
        }

        var cssLinks = doc.DocumentNode.Descendants("link");
        foreach (var cssLink in cssLinks)
        {
            string href = cssLink.GetAttributeValue("href", null);
            if (!string.IsNullOrEmpty(href) && IsHttpOrHttpsLink(href))
            {
                links.Add(href);
            }
        }

        return links;
    }
    
    private static bool IsHttpOrHttpsLink(string link)
    {
        return link.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || link.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
    }

    public async Task<List<(string Url, string PhoneNumber)>> ScrapePhoneNumbers(List<string> urls, int maxPhoneNumbers)
    {
        List<(string Url, string PhoneNumber)> phoneNumbers = new List<(string Url, string PhoneNumber)>();
        int count = 0;

        foreach (string url in urls)
        {
            if (count >= maxPhoneNumbers)
                break;

            string html = await _httpClient.GetStringAsync(url);
            var matches = Regex.Matches(html, @"((\+86|86)?\s*1[3456789]\d{9})|((\+86|86)?\s*(\(0\d{2,3}\)|0\d{2,3}-)?([2-9]\d{6,7})(-\d{1,4})?)");

            foreach (Match match in matches)
            {
                string phoneNumber = match.Value;
                phoneNumbers.Add((url, phoneNumber));
                count++;

                if (count >= maxPhoneNumbers)
                    break;
            }
        }
        return phoneNumbers;
    }
    
}
